﻿// QueryForm.cs
using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;

namespace YouGeBookmark
{
    public partial class QueryForm : Form
    {
        private int _currentPage = 1;
        private int _pageSize = 20;

        public QueryForm()
        {
            InitializeComponent();
            LoadTags();
            dtpStart.Value = DateTime.Now.AddMonths(-1);
            dtpEnd.Value = DateTime.Now;
        }

        // QueryForm.cs 补充代码
        private void LoadTags()
        {
            clbTags.Items.Clear();

            using (var conn = new SQLiteConnection("Data Source=bookmarks.db;Version=3;"))
            {
                conn.Open();

                // 创建临时标签表（如果不存在）
                string createTableSql = @"CREATE TABLE IF NOT EXISTS Tags (
                                    ID INTEGER PRIMARY KEY AUTOINCREMENT,
                                    Name TEXT NOT NULL UNIQUE
                                 );";
                new SQLiteCommand(createTableSql, conn).ExecuteNonQuery();

                // 插入初始默认标签（仅首次运行时）
                string initTagsSql = @"INSERT OR IGNORE INTO Tags (Name) 
                             VALUES ('时政'),('财经'),('军事'),('科技'),('行业')";
                new SQLiteCommand(initTagsSql, conn).ExecuteNonQuery();

                // 加载所有标签
                string selectSql = "SELECT Name FROM Tags ORDER BY Name";
                using (var cmd = new SQLiteCommand(selectSql, conn))
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        clbTags.Items.Add(reader["Name"].ToString());
                    }
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            _currentPage = 1;
            LoadData();
        }

        private void LoadData()
        {
            using var conn = new SQLiteConnection("Data Source=bookmarks.db;Version=3;");
            conn.Open();

            string sql = @"SELECT URL, Tags, Notes, CreateDate 
                         FROM Bookmarks 
                         WHERE CreateDate BETWEEN @start AND @end
                         LIMIT @pageSize OFFSET @offset";

            var cmd = new SQLiteCommand(sql, conn);
            cmd.Parameters.AddWithValue("@start", dtpStart.Value);
            cmd.Parameters.AddWithValue("@end", dtpEnd.Value);
            cmd.Parameters.AddWithValue("@pageSize", _pageSize);
            cmd.Parameters.AddWithValue("@offset", (_currentPage - 1) * _pageSize);

            var adapter = new SQLiteDataAdapter(cmd);
            var dt = new DataTable();
            adapter.Fill(dt);
            dgvResults.DataSource = dt;

            UpdatePageInfo();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                LoadData();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            _currentPage++;
            LoadData();
        }

        // Windows 窗体设计器生成的代码
        private DataGridView dgvResults;
        private DateTimePicker dtpStart;
        private DateTimePicker dtpEnd;
        private CheckedListBox clbTags;
        private Button btnSearch;
        private Button btnPrev;
        private Button btnNext;

        private Label lblStartDate;
        private Label lblEndDate;
        private Label lblTagsFilter;
        private Label lblPageInfo;

        // QueryForm.cs 完整布局
        private void InitializeComponent()
        {
            dgvResults = new DataGridView();
            dtpStart = new DateTimePicker();
            dtpEnd = new DateTimePicker();
            clbTags = new CheckedListBox();
            btnSearch = new Button();
            btnPrev = new Button();
            btnNext = new Button();
            lblStartDate = new Label();
            lblEndDate = new Label();
            lblTagsFilter = new Label();
            lblPageInfo = new Label();
            ((System.ComponentModel.ISupportInitialize)dgvResults).BeginInit();
            SuspendLayout();
            // 
            // dgvResults
            // 
            dgvResults.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            dgvResults.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dgvResults.Location = new System.Drawing.Point(12, 120);
            dgvResults.Name = "dgvResults";
            dgvResults.RowTemplate.Height = 25;
            dgvResults.Size = new System.Drawing.Size(660, 300);
            dgvResults.TabIndex = 0;
            // 
            // dtpStart
            // 
            dtpStart.Format = DateTimePickerFormat.Short;
            dtpStart.Location = new System.Drawing.Point(12, 32);
            dtpStart.Name = "dtpStart";
            dtpStart.Size = new System.Drawing.Size(120, 23);
            dtpStart.TabIndex = 1;
            // 
            // dtpEnd
            // 
            dtpEnd.Format = DateTimePickerFormat.Short;
            dtpEnd.Location = new System.Drawing.Point(150, 32);
            dtpEnd.Name = "dtpEnd";
            dtpEnd.Size = new System.Drawing.Size(120, 23);
            dtpEnd.TabIndex = 2;
            // 
            // clbTags
            // 
            clbTags.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
            clbTags.FormattingEnabled = true;
            clbTags.Location = new System.Drawing.Point(12, 80);
            clbTags.Name = "clbTags";
            clbTags.Size = new System.Drawing.Size(180, 112);
            clbTags.TabIndex = 3;
            // 
            // btnSearch
            // 
            btnSearch.Location = new System.Drawing.Point(300, 32);
            btnSearch.Name = "btnSearch";
            btnSearch.Size = new System.Drawing.Size(75, 30);
            btnSearch.TabIndex = 4;
            btnSearch.Text = "搜索";
            btnSearch.UseVisualStyleBackColor = true;
            btnSearch.Click += btnSearch_Click;
            // 
            // btnPrev
            // 
            btnPrev.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            btnPrev.Location = new System.Drawing.Point(12, 426);
            btnPrev.Name = "btnPrev";
            btnPrev.Size = new System.Drawing.Size(75, 30);
            btnPrev.TabIndex = 5;
            btnPrev.Text = "上一页";
            btnPrev.UseVisualStyleBackColor = true;
            btnPrev.Click += btnPrev_Click;
            // 
            // btnNext
            // 
            btnNext.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            btnNext.Location = new System.Drawing.Point(597, 426);
            btnNext.Name = "btnNext";
            btnNext.Size = new System.Drawing.Size(75, 30);
            btnNext.TabIndex = 6;
            btnNext.Text = "下一页";
            btnNext.UseVisualStyleBackColor = true;
            btnNext.Click += btnNext_Click;
            // 
            // lblStartDate
            // 
            lblStartDate.Location = new System.Drawing.Point(0, 0);
            lblStartDate.Name = "lblStartDate";
            lblStartDate.Size = new System.Drawing.Size(100, 23);
            lblStartDate.TabIndex = 0;
            // 
            // lblEndDate
            // 
            lblEndDate.Location = new System.Drawing.Point(0, 0);
            lblEndDate.Name = "lblEndDate";
            lblEndDate.Size = new System.Drawing.Size(100, 23);
            lblEndDate.TabIndex = 0;
            // 
            // lblTagsFilter
            // 
            lblTagsFilter.Location = new System.Drawing.Point(0, 0);
            lblTagsFilter.Name = "lblTagsFilter";
            lblTagsFilter.Size = new System.Drawing.Size(100, 23);
            lblTagsFilter.TabIndex = 0;
            // 
            // lblPageInfo
            // 
            lblPageInfo.Location = new System.Drawing.Point(0, 0);
            lblPageInfo.Name = "lblPageInfo";
            lblPageInfo.Size = new System.Drawing.Size(100, 23);
            lblPageInfo.TabIndex = 0;
            // 
            // QueryForm
            // 
            AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new System.Drawing.Size(684, 461);
            Controls.Add(btnNext);
            Controls.Add(btnPrev);
            Controls.Add(btnSearch);
            Controls.Add(clbTags);
            Controls.Add(dtpEnd);
            Controls.Add(dtpStart);
            Controls.Add(dgvResults);
            MinimumSize = new System.Drawing.Size(700, 500);
            Name = "QueryForm";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "书签查询";
            Load += QueryForm_Load;
            ((System.ComponentModel.ISupportInitialize)dgvResults).EndInit();
            ResumeLayout(false);
        }

        private void UpdatePageInfo()
        {
            lblPageInfo.Text = $"第 {_currentPage} 页";
        }

        private void QueryForm_Load(object sender, EventArgs e)
        {

        }
    }
}